package javaClassExercise.file;

/**
 * Created by lomo on 2017/7/20.
 */
public class fileMD5 {

//
//    public static void main(String[] args) {
//        try {
//            File file = new File("/Users/lomo/Downloads/enterpriseLoanDetail/views/extendTermWhat.html");
//            FileInputStream fis = new FileInputStream(file);
//            MessageDigest md = MessageDigest.getInstance("MD5");
//            byte[] buffer = new byte[1024];
//            int length = -1;
//            while ((length = fis.read(buffer, 0, 1024)) != -1) {
//                md.update(buffer, 0, length);
//            }
//            BigInteger bigInt = new BigInteger(1, md.digest());
//            System.out.println("文件md5值：" + bigInt.toString(16));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

/*
    public static byte[] createChecksum(String filename) throws Exception {
        InputStream fis =  new FileInputStream(filename);

        byte[] buffer = new byte[1024];
        MessageDigest complete = MessageDigest.getInstance("MD5");
        int numRead;

        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);

        fis.close();
        return complete.digest();
    }

    // see this How-to for a faster way to convert
    // a byte array to a HEX string
    public static String getMD5Checksum(String filename) throws Exception {
        byte[] b = createChecksum(filename);
        String result = "";

        for (int i=0; i < b.length; i++) {
            result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
        return result;
    }

    public static void main(String args[]) {
        try {
            System.out.println(getMD5Checksum("/Users/lomo/Downloads/enterpriseLoanDetail/views/extendTermWhat.html"));
            // output :
            //  0bb2827c5eacf570b6064e24e0e6653b
            // ref :
            //  http://www.apache.org/dist/
            //          tomcat/tomcat-5/v5.5.17/bin
            //              /apache-tomcat-5.5.17.exe.MD5
            //  0bb2827c5eacf570b6064e24e0e6653b *apache-tomcat-5.5.17.exe
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    */




}
